from celery import shared_task
from .models import City, Room, Device, DailySummary
from django.utils import timezone


@shared_task
def daily_task():
    today = timezone.now().date()
    for city in City.objects.all():
        rooms = Room.objects.filter(city=city)
        for room in rooms:
            count = Device.objects.filter(room=room).count()
            DailySummary.objects.update_or_create(
                city=city,
                room=room,
                date=today,
                count=count
            )